package com.jiantong.work.impl.service;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.enums.FeedReportStatus;
import com.jiantong.work.impl.dao.FeedReportDao;
import com.jiantong.work.model.FeedReport;
import com.jiantong.work.service.FeedReportReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Slf4j
@Service
@RpcProvider
public class FeedReportReadServiceImpl implements FeedReportReadService {
    private final FeedReportDao feedReportDao;
    @Autowired
    public FeedReportReadServiceImpl(FeedReportDao feedReportDao) {
        this.feedReportDao = feedReportDao;
    }

    @Override
    public Response<FeedReport> findById(Long id) {
        try {
            FeedReport feedReport = feedReportDao.findById(id);
            if (feedReport == null) {
                log.error("feedReport(id={}) is not found", id);
                return Response.fail("feedReport.not.found");
            }
            return Response.ok(feedReport);
        } catch (Exception e) {
            log.error("failed to find feedReport(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("feedReport.find.fail");
        }
    }

    @Override
    public Response<Map<String, Object>> findStatusById(Long id) {
        try{
            Integer status = feedReportDao.findStatusById(id);
            Map<String , Object> map = Maps.newHashMap();
            if(status == null){
                log.error("feedReport status (id={}) is not found", id);
                return Response.fail("feedReport.status.not.found");
            }
            map.put("status",status);
            return Response.ok(map);
        }catch (Exception e){
            log.error("feedReport status (id={}) is not found", id);
            return Response.fail("feedReport.status.not.found");
        }
    }

    @Override
    public Response<Paging<FeedReport>> pagingFeedReport(Integer pageNo, Integer pageSize, Integer status,Long breedFactoryId) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();

            criteria.put("breedFactoryId",breedFactoryId);

            if (status !=null) criteria.put("status", status);

            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(feedReportDao.paging(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find feedReport(id={}), cause:{}", status, Throwables.getStackTraceAsString(e));
            return Response.fail("feedReport.find.fail");
        }
    }

    @Override
    public Response<Paging<FeedReport>> pagingFeedReports(Integer pageNo, Integer pageSize, Long breedFactoryId,Integer feedType, Date fromDate, Date toDate) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();

            List<Long> breedFactoryIds = new ArrayList<>();
            if(breedFactoryId!=null){
                breedFactoryIds.add(breedFactoryId);
            }else {
                LoginUser loginUser = UserUtil.getCurrentUser();
                breedFactoryIds = loginUser.getBreedFactoryIds();
            }
            if(!breedFactoryIds.isEmpty()){
                criteria.put("breedFactoryIds",breedFactoryIds);
            }else {
                return Response.ok(new Paging<>(0L,new ArrayList<>()));
            }

            criteria.put("feedType",feedType);                //添加的养殖类型查询条件
            if(fromDate!=null) criteria.put("fromDate", fromDate);

            if(toDate != null) criteria.put("toDate",toDate);

            criteria.put("status", FeedReportStatus.FINISHED.value());
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(feedReportDao.pagingFeedReports(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find pollutionControl fromDate={},fromDate={},toDate={}),cause:{}", fromDate,toDate,Throwables.getStackTraceAsString(e));
            return Response.fail("pollutionControl.find.fail");
        }
    }

    /**
     *根据时间起始 饲料种类查询
     */

    public Response<Paging<FeedReport>> pagingbytimes (Integer pageNo, Integer pageSize, Map<String,Object> criteria){

        try{ PageInfo pageInfo = new PageInfo(pageNo, pageSize);

            return Response.ok(feedReportDao.pagingbytimes(pageInfo.getOffset(), pageInfo.getLimit(),criteria));


        }catch(Exception e){
            log.error("failed to find pagingbytimes{}, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("pagingbytimes.find.fail");
        }

    }

    @Override
    public Response<List<Map<String , Object>>> findSumForWeb(Map<String, Object> criteria) {
        try{
            return Response.ok(feedReportDao.findSumForWeb(criteria));
        }catch (Exception e){
            log.error("failed to find sum info , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find sum info");
        }
    }

    @Override
    public Response<Integer> countForWeb(Map<String, Object> map) {
        try{
            return Response.ok(feedReportDao.countForWeb(map));
        }catch (Exception e){
            log.error("failed to count feed report times , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to count feed report times");
        }
    }
}
